﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WLMESService
 * 唯一标识：6a99f7d5-304a-4e52-8833-cdf9756d3044
 * 文件名：BMesWebService
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/10/28 8:47:46
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>


using K8.CBB;

namespace KingerRobot.SailunMESService;

public class BMesWebService : IMesWebService
{
    private readonly AsrsWebSerivcePortTypeClient portTypeClient = new AsrsWebSerivcePortTypeClient();

    public int PushAbnormal(string barCode, long startLoc, long targetLoc)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"16.异常输送（WMS->MES）{barCode},{startLoc},{targetLoc}" });

            portTypeClient.transferspecialAsync(barCode, startLoc.ToString(), targetLoc.ToString());
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 1;
    }

    public int PushDirectDelivery(PushDirectDeliveryMesParam pushDirectDeliveryMesParam)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"11.直送信息（WMS->MES）{pushDirectDeliveryMesParam.ToJson()}" });

            var barCode = pushDirectDeliveryMesParam.BarCode;
            var detailId = pushDirectDeliveryMesParam.OrderDetailId;
            var loc = pushDirectDeliveryMesParam.CurrentLoc;
            var equipNo = pushDirectDeliveryMesParam.EquipNo;
            var equipModel = pushDirectDeliveryMesParam.EquipModel;
            portTypeClient.outbounddirectAsync(barCode, detailId, loc, equipNo, equipModel);
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 0;
    }

    public int PushDirectDeliveryFinish(PushDirectDeliveryFinishMesParam pushDirectDeliveryFinishMesParam)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $" 13.直送完成（WMS->MES）{pushDirectDeliveryFinishMesParam.ToJson()}" });

            var barCode = pushDirectDeliveryFinishMesParam.BarCode;
            var detailId = pushDirectDeliveryFinishMesParam.OrderDetailId;
            var equipNo = pushDirectDeliveryFinishMesParam.EquipNo;
            var equipModel = pushDirectDeliveryFinishMesParam.EquipModel;
            portTypeClient.oubounddirectcompleteAsync(barCode, detailId, equipNo, equipModel);
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 0;
    }

    public int PushOutOrderStatus(List<PushOutOrderStatusMesParam> pushOutOrderStatusMesParams)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"8.出库订单状态（WMS->MES）出库订单发生变化时,推送出库订单状态{pushOutOrderStatusMesParams.ToJson()}" });
            pushOutOrderStatusMesParams.ForEach(item =>
            {
                var orderDetailId = item.OrderDetailId;
                var orderStatus = item.OrderStatus;
                var tireBarcode = item.TireBarcode;

                var taskResult = portTypeClient.updateoutputstateAsync(orderDetailId, orderStatus, tireBarcode);
            });
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 1;
    }

    public int PushWarehousing(string tireBarcode, string currentLoc)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $" 9.入库信息（WMS->MES）胎胚扫码后，进行胎胚入库，由WMS将信息推送至MES{tireBarcode},{currentLoc}" });

            portTypeClient.inboundAsync(tireBarcode, currentLoc);
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 1;
    }

    public int PushWarehousingFinish(string tireBarcode, string currentLoc)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"12.入库完成（WMS->MES）堆垛机入库任务完成后，由WMS将信息推送至MES{tireBarcode},{currentLoc}" });

            portTypeClient.inboundcompleteAsync(tireBarcode, currentLoc);
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 1;
    }

    public int PushWarehousingOut(List<PushWarehousingOutMesParam> pushWarehousingOutMesParams)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"10.出库信息（WMS->MES）开始执行堆垛机出库任务，由WMS将信息推送至MES{pushWarehousingOutMesParams.ToJson()}" });

            pushWarehousingOutMesParams.ForEach(item =>
            {
                var tireBarcode = item.TireBarcode;
                var orderDetailId = item.OrderDetailId;
                var currentLoc = item.CurrentLoc;
                var equipNo = item.EquipNo;
                var equipModel = item.EquipModel.ToString();
                var targetLoc = item.TargetLoc;
                portTypeClient.outboundAsync(tireBarcode, orderDetailId, currentLoc, equipNo, equipModel, targetLoc);
            });
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }

        return 1;
    }

    public int PushWarehousingOutFinish(List<PushWarehousingOutFinishMesParam> pushWarehousingOutFinishMesParams)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"14.出库完成（WMS->MES）出库任务EMS放胎完成到达硫化机后，由WMS将信息推送至MES{pushWarehousingOutFinishMesParams.ToJson()}" });

            pushWarehousingOutFinishMesParams.ForEach(item =>
            {
                var tireBarcode = item.TireBarcode;
                var orderDetailId = item.OrderDetailId;
                var equipNo = item.EquipNo;
                var equipModel = item.EquipModel;
                var targetLoc = item.TargetLoc;
                portTypeClient.outboundcompleteAsync(tireBarcode, orderDetailId, equipNo, equipModel, targetLoc);
            });
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = ex.Message });
        }
        return 1;
    }


    public ProductGreenTireParam ProductGreenTire(string productCode)
    {
        try
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $" 4.胎胚信息（WMS->MES） WMS拉取胎胚物料信息:{productCode}" });

            var result = portTypeClient.synGreentyreInfoAsync(productCode);
            var bean = result.Result;

            if (bean is not null)
            {
                DateTime.TryParse(bean.product_time, out DateTime dateTime);
                int.TryParse(bean.tire_lock, out int tirelock);
                int.TryParse(bean.quailty_status, out int status);

                return new()
                {
                    TireBarcode = bean.tire_barcode,
                    MaterialId = bean.material_id,
                    ProductEquip = bean.product_equip,
                    ProductTime = dateTime,
                    ProductBatch = bean.product_batch,
                    TireLock = tirelock,
                    QuailtyStatus = status,
                };
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"productCode:{productCode},{ex.Message}" });
        }

        return null;
    }

    public int test666()
    {

        return 666;
    }
}
